package com.huhao.smark;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class LedgerFragment extends Fragment {
    private FrameLayout fragmentContainer;
    private Button detailButton;
    private Button financeButton;
    private int currentPage;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ledger, container, false);

        detailButton = view.findViewById(R.id.detail_button);
        financeButton = view.findViewById(R.id.finance_button);
        fragmentContainer = view.findViewById(R.id.container);

        detailButton.setOnClickListener(v -> {
            ((MainActivity) requireActivity()).showLedgerPage(0);
            currentPage = 0;
            setButtonColors();
        });
        financeButton.setOnClickListener(v -> {
            ((MainActivity) requireActivity()).showLedgerPage(1);
            currentPage = 1;
            setButtonColors();
        });

        ((MainActivity) requireActivity()).showLedgerPage(0);
        currentPage = 0;
        setButtonColors();

        return view;
    }

    public FrameLayout getFragmentContainer() {
        return fragmentContainer;
    }

    private void setButtonColors() {
        Context context = getContext();
        if (context != null) {
            // 设置背景颜色
            int[] selectedColors = {ContextCompat.getColor(context, R.color.default_color), ContextCompat.getColor(context, R.color.white)};
            int[] unselectedColors = {ContextCompat.getColor(context, R.color.default_color), ContextCompat.getColor(context, R.color.default_color)};
            GradientDrawable selectedBackground = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, selectedColors);
            GradientDrawable unselectedBackground = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, unselectedColors);

            // 设置文字和图标颜色
            int selectedTextColor = ContextCompat.getColor(context, R.color.black);
            int unselectedTextColor = ContextCompat.getColor(context, R.color.gray);

            if (currentPage == 0) {
                // 明细按钮选中
                detailButton.setBackground(selectedBackground);
                detailButton.setTextColor(selectedTextColor);
                detailButton.setCompoundDrawablesWithIntrinsicBounds(null, getTintedDrawable(R.drawable.ic_detail, selectedTextColor), null, null);

                financeButton.setBackground(unselectedBackground);
                financeButton.setTextColor(unselectedTextColor);
                financeButton.setCompoundDrawablesWithIntrinsicBounds(null, getTintedDrawable(R.drawable.ic_finance, unselectedTextColor), null, null);
            } else {
                // 统计按钮选中
                detailButton.setBackground(unselectedBackground);
                detailButton.setTextColor(unselectedTextColor);
                detailButton.setCompoundDrawablesWithIntrinsicBounds(null, getTintedDrawable(R.drawable.ic_detail, unselectedTextColor), null, null);

                financeButton.setBackground(selectedBackground);
                financeButton.setTextColor(selectedTextColor);
                financeButton.setCompoundDrawablesWithIntrinsicBounds(null, getTintedDrawable(R.drawable.ic_finance, selectedTextColor), null, null);
            }
        }
    }

    // 创建带有颜色过滤的 Drawable
    private Drawable getTintedDrawable(int drawableRes, int color) {
        Drawable drawable = ContextCompat.getDrawable(requireContext(), drawableRes);
        if (drawable != null) {
            drawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
        }
        return drawable;
    }
}